package com.runshu.newsmartark.util.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.viewpager.widget.ViewPager;

/**
 * Demo class
 *
 * @author yyd
 * @date 2018/5/25
 */
public class NoScrollViewPager extends ViewPager {
    private boolean noScroll = false;
    private boolean isSmooth = false;

    /** 是否有过渡动画 默认是没有
     * @param smooth
     */
    public void setSmooth (boolean smooth) {
        isSmooth = smooth;
    }

    public NoScrollViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    public NoScrollViewPager(Context context) {
        super(context);
    }


    /** 是否是不滑动  默认是false 是滑动的
     * @param noScroll
     */
    public void setNoScroll (boolean noScroll) {
        this.noScroll = noScroll;
    }


    @Override
    public void scrollTo (int x, int y) {
        super.scrollTo(x, y);
    }


    @Override
    public boolean onTouchEvent (MotionEvent arg0) {
        if (noScroll)
            return false;
        else
            return super.onTouchEvent(arg0);
    }


    @Override
    public boolean onInterceptTouchEvent (MotionEvent arg0) {
        if (noScroll)
            return false;
        else
            return super.onInterceptTouchEvent(arg0);
    }


    @Override
    public void setCurrentItem (int item, boolean smoothScroll) {
        if (! isSmooth) {
            super.setCurrentItem(item, false);
        } else {
            super.setCurrentItem(item, smoothScroll);
        }
    }


    @Override
    public void setCurrentItem (int item) {
        if (isSmooth) {
            super.setCurrentItem(item);
        } else {
            super.setCurrentItem(item, false);//表示切换的时候，不需要切换时间。
        }
    }


}
